﻿using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
using System.IO;

namespace QG.FrontDesk.Remote
{
    public class TenXunYunSMS
    {
        //appId
        public int appId;
        //appKey
        public string appKey;
        //短信模板ID
        private int tmplateId = 476304;
        //签名内容
        private string smsSign = "t18182第三组";

        /// <summary>
        /// 验证码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool SetSMS(string phone)
        {
            try
            {
                SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
                var result = ssender.sendWithParam("86", phone,
                    tmplateId, new[] { Code.ToString(), "5" }, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
                return true;
            }
            catch (JSONException ex)
            {
                //拼接字符串
                string msg = $"时间:{DateTime.Now},{ex.Message}";
                //写入路径，应配置Web.config
                string path = @"C:\\log.txt";
                //写入文件
                File.AppendAllText(path, msg);
                return false;
            }
            catch (HTTPException ex)
            {
                //拼接字符串
                string msg = $"时间:{DateTime.Now},{ex.Message}";
                //写入路径，应配置Web.config
                string path = @"C:\\log.txt";
                //写入文件
                File.AppendAllText(path, msg);
                return false;
            }
            catch (Exception ex)
            {
                //拼接字符串
                string msg = $"时间:{DateTime.Now},{ex.Message}";
                //写入路径，应配置Web.config
                string path = @"C:\\log.txt";
                //写入文件
                File.AppendAllText(path, msg);
                return false;
            }
        }
    }
}
